<template>
  <div>
    <template>
      <div class="app-container">
        <el-form :inline="true" :model="searchObj" class="demo-form-inline">
          <el-form-item label="医院名称">
            <el-input v-model="searchObj.hosname" ></el-input>
          </el-form-item>
          <el-form-item label="医院编号">
            <el-input v-model="searchObj.hoscode" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
        <!-- 工具条 -->
        <div>
          <el-button type="danger" size="mini" @click="removeRows()">批量删除</el-button>
        </div>

        <!-- banner列表 -->
        <el-table
          :data="list"
          stripe
          style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column type="index" width="50"/>
          <el-table-column prop="hosname" label="医院名称"/>
          <el-table-column prop="hoscode" label="医院编号"/>
          <el-table-column prop="apiUrl" label="api基础路径"width="200"/>
          <el-table-column prop="contactsName" label="联系人姓名"/>
          <el-table-column prop="contactsPhone" label="联系人手机"/>
          <el-table-column label="状态" width="50">
            <template slot-scope="scope">
              {{ scope.row.status === 1 ? '可用' : '不可用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="280" align="center">
            <template slot-scope="scope">
              <el-button type="danger" size="mini"
                         icon="el-icon-delete" @click="removeDataById(scope.row.id)"> </el-button>
              <el-button v-if="scope.row.status==1" type="primary" size="mini"
                         icon="el-icon-delete" @click="lockHostSet(scope.row.id,0)">锁定</el-button>
              <el-button v-if="scope.row.status==0" type="danger" size="mini"
                         icon="el-icon-delete" @click="lockHostSet(scope.row.id,1)">取消锁定</el-button>
              <router-link :to="'/hospSet/edit/'+scope.row.id">
                <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
              </router-link>

            </template>
          </el-table-column>

        </el-table>
        <el-pagination
          :current-page="current"
          :page-size="limit"
          :total="total"
          style="padding: 30px 0; text-align: center;"
          layout="total, prev, pager, next, jumper"
          @current-change="fetchData"
        />

      </div>
    </template>

  </div>
</template>
<script>
  import hospSet from '../../api/hospSet'
  export default {
    data() {
      return {
        current: 1, //当前页
        limit: 3,  //
        searchObj: {
          hosname: '',
          hoscode: ''
        },
        list: [],
        total: 0,
        multipleSelection: [] // 批量选择中选择的记录列表
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        hospSet.getHospSetList(this.current, this.limit, this.searchObj).then(res => {
          this.list = res.data.records;
          this.total = res.data.total;
        }).catch(error => {
          console.log(error)
        })
      },
      fetchData(newPage) {
        this.current = newPage;
        this.getList()
      },
      search() {
        this.current = 1
        this.getList()
      },
      removeDataById(id) {
        this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          hospSet.deleteById(id).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功'
              });
              this.current = 1
              this.getList() //刷新信息
            } else {
              this.$message.error('删除失败');
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSelectionChange(selection) {
        this.multipleSelection = selection;
      },
      removeRows() {
        if (this.multipleSelection.length) {
          this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            var ids = []
            for (var i = 0; i < this.multipleSelection.length; i++) {
              var obj = this.multipleSelection[i]
              var id = obj.id
              ids.push(id)
            }
            hospSet.deleteBatch(ids).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功'
              });
              this.current = 1
              this.getList() //刷新信息
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        }
      },
      lockHostSet(id,status){
        hospSet.lockHospSet(id,status).then(res=>{
            this.$message({
              type: 'success',
              message: '锁定成功'
            })
          this.current=1
          this.getList()
        })
      }
    }
  }
</script>
